package me.firstapp.manage.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.qiniu.util.Auth;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;

@Controller
@RequestMapping(value = "/upload")
public class UploadAct extends BaseManage {
	
	@Value("#{configProperties['qiniu.upload.accessKey']}")
	private String accessKey;

	@Value("#{configProperties['qiniu.upload.secret']}")
	private String secret;

	@Value("#{configProperties['qiniu.upload.bucketname']}")
	private String bucketName;

	@Value("#{configProperties['qiniu.upload.domain']}")
	private String domain;

	@RequestMapping(value = "/getUploadToken.do", method = RequestMethod.GET)
	public void getUploadToken(HttpServletRequest request, HttpServletResponse response) {
		SingleObject<String> result = new SingleObject<>();
		try {
			String uploadToken = Auth.create(accessKey, secret).uploadToken(bucketName);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(uploadToken);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
